package login

/*******************************************************************************
Copyright:cloud
Author:cloudapex@126.com
Version:1.0
Date:2020-06-12
Description: 登陆拉取权限相关
*******************************************************************************/
import (
	"context"
	"service/conf"
	"service/model/mod"
	model "service/model/permis"

	gpb "gitee.com/cloudapex/protos/permis/pb"

	"gitee.com/cloudapex/ulern/htp"
	"gitee.com/cloudapex/ulern/util"
	"github.com/gin-gonic/gin"
)

/*
 * 描述: 登陆时的权限数据
 * 路由: /login/permis
 * 方法: GET
 *********************************************************************/
type MyPermis struct {
}

func (this *MyPermis) Handle(c *gin.Context, ctx context.Context) htp.Response {
	user := mod.CurrUser(c)

	type result struct {
		Admin int32 `json:"admin"` // 是否是超级管理员 0:不是; 1:是
		gpb.MyPermisRes
	}

	// 检测超级管理员
	ret := result{}
	if util.ContainI(user.UId, conf.Main.AdminIds()...) {
		return htp.RespOK("", &result{Admin: 1})
	}

	//htp.TranGHead(ctx, c, "User-Agent", "User-Device")
	rsp, err := model.Login.MyPermis(ctx, &gpb.MyPermisReq{
		UId:        user.UId,
		UserAgent:  c.GetHeader("User-Agent"),
		UserDevice: c.GetHeader("User-Device"),
	})
	if err != nil {
		return htp.RespModelErr("", err)
	}
	if err := util.Convert(&ret, rsp); err != nil {
		return htp.RespModelErr("", err)
	}
	return htp.RespOK("", &ret)
}
